package org.vectrics.recruiter.struts;

import java.io.FileOutputStream;
import java.io.InputStream;

import org.apache.log4j.Category;
import org.apache.struts.upload.FormFile;
import org.vectrics.recruiter.RecruitServiceLocator;
import org.vectrics.recruiter.config.RecruitConfig;
import org.vectrics.recruiter.user.CandidateInfo;
import org.vectrics.recruiter.user.Resume;
import org.vectrics.user.Person;


public class ResumeUploader {
    private static Category log = Category.getInstance(ResumeUploader.class);
	private FormFile formFile = null;
	private Person person = null;
	private CandidateInfo candidate = null;
	private String resumeName = null;
	private String ext = null;

	
	public ResumeUploader(CandidateInfo candidate, FormFile formFile, String resumeName) {
	    this.formFile = formFile;
	    this.resumeName = resumeName;
	    this.person = candidate.getPerson();
	    this.candidate = candidate;
	}
	
	public void validate() throws IllegalResumeFileTypeException {
		String fileName = formFile.getFileName();
	    if (this.isFileTypeLegal()) {
		    throw new IllegalResumeFileTypeException("The type of the file was illegal: " + fileName);
	    }
	}

	// Intercept 
	public void load() throws IllegalResumeFileTypeException, ResumeFileSaveException {
		boolean illegalFileType = isFileTypeLegal();
		
		String fileName = formFile.getFileName();
		if (log.isDebugEnabled()) {
			log.debug("execute() - uploading resume for user: " + person.getName());
			log.debug("File Name = " + fileName);
			log.debug("File Size = " + formFile.getFileSize());
		}
		
		
		if (resumeName == null) {
			resumeName = "Uploaded File";
		}
		
		if (!illegalFileType) {
			try {
				Resume resume = candidate.addResume();
				resume.setName(resumeName);
				String resumeFileName = resume.getId() + "." + ext; 
				String fileNameAndPath = RecruitConfig.getRecruitInstance().getCandidateDocumentPath(this.candidate.getPerson().getDomain())
							+ "/" + resumeFileName;
				FileOutputStream file = new FileOutputStream(fileNameAndPath);
				InputStream uploadStream = formFile.getInputStream();
				
				resume.setName(resumeName);
				RecruitServiceLocator.getRecruitUserService().saveResumeToFile(candidate, resume, formFile.getFileName(), uploadStream);
				RecruitServiceLocator.getRecruitUserService().updateCandidateInfo(candidate);
				log.info("File saved successfully");
			}
			catch (Exception ex) {
				log.error("Exception saving file to server drive", ex);
				throw new ResumeFileSaveException("Exception saving file to server drive", ex);
			}
		} else {
		    throw new IllegalResumeFileTypeException("The type of the file was illegal: " + fileName);
		}
	 }
	
	
	 protected boolean isFileTypeLegal() {
	     boolean illegalFileType = false;
		 String fileName = formFile.getFileName();
		 ext = "doc";
		 if (fileName.endsWith("pdf")) {
			ext = "pdf";
		 } else if (fileName.endsWith("txt")) {
			ext = "txt";
		 } else if (fileName.endsWith("doc")) {
			ext = "doc";
		 } else {
			illegalFileType = true;
		 }
		 return(illegalFileType);
	 }
	 
		
	 
    /**
     * @return Returns the formFile.
     */
    public FormFile getFormFile() {
        return formFile;
    }
    /**
     * @param formFile The formFile to set.
     */
    public void setFormFile(FormFile formFile) {
        this.formFile = formFile;
    }
    /**
     * @return Returns the person.
     */
    public Person getPerson() {
        return person;
    }
    /**
     * @param person The person to set.
     */
    public void setPerson(Person person) {
        this.person = person;
    }
    /**
     * @return Returns the resumeName.
     */
    public String getResumeName() {
        return resumeName;
    }
    /**
     * @param resumeName The resumeName to set.
     */
    public void setResumeName(String resumeName) {
        this.resumeName = resumeName;
    }
}
